package com.hongyun.tms.exception;

import com.hongyun.tms.common.enums.BizCode;
import org.apache.commons.lang3.StringUtils;

/**
 * 业务异常类
 *
 * @author lipeng
 * @date 2024-05-31
 **/
public final class BusinessRuntimeException extends RuntimeException {

    private static final long serialVersionUID = 4236266714060887509L;

    private BizCode errorCode;

    private String detailMsg;


    public BusinessRuntimeException() {
        super();
    }

    public BusinessRuntimeException(String msg) {
        super(msg);
    }

    //************************ The following is the optimize***********************
    public BusinessRuntimeException(BizCode errorCode) {
        this.errorCode = errorCode;
    }

    public BusinessRuntimeException(BizCode errorCode, String detailMsg) {
        this.errorCode = errorCode;
        this.detailMsg = detailMsg;
    }

    public BusinessRuntimeException(BizCode errorCode, Throwable t) {
        super(t);
        this.errorCode = errorCode;
    }

    public BusinessRuntimeException(BizCode errorCode, String detailMsg, Throwable t) {
        super(t);
        this.errorCode = errorCode;
        this.detailMsg = detailMsg;
    }

    public BizCode getErrorCode() {
        return errorCode;
    }

    @Override
    public String getMessage() {

        String detail = "";
        if (StringUtils.isNotBlank(detailMsg)) {
            detail += detailMsg;
        }

        String superMsg = super.getMessage();
        if (StringUtils.isNotBlank(superMsg)) {
            detail += ", " + superMsg;
        }

        return String.format("code=[%s], msg=[%s], detailMsg=[%s]", errorCode.name(), errorCode.getComment(), detail);
    }

    public String getErrorDesc() {
        return errorCode.getComment();
    }

    public String getErrorDetailMsg() {
        return detailMsg;
    }

    public String getErrorDescWithDetail() {
        return errorCode.getComment() + "," + detailMsg;
    }

}
